package com.clj.handler;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;

import com.clj.entity.StudentDO_clj;
import com.clj.service.StudentService_clj;
import com.clj.service.impl.StudentServiceCljImpl_clj;
import com.clj.view.AddStudentView_clj;
import com.clj.view.MainView_clj;

/**
 * Author: Harry Chen
 * Date: 2023/02/24 15:59
 * Description: 添加按钮的监听器
 */
public class AddStudentViewHandler_clj implements ActionListener {
    private AddStudentView_clj addStudentViewClj;
    private MainView_clj mainViewClj;

    public AddStudentViewHandler_clj(AddStudentView_clj addStudentViewClj, MainView_clj mainViewClj) {
        this.addStudentViewClj = addStudentViewClj;
        this.mainViewClj = mainViewClj;
    }

    public void actionPerformed(ActionEvent e) {
        JButton jButton = (JButton) e.getSource();
        String text = jButton.getText();
        if ("添加".equals(text)) {
            StudentService_clj studentServiceClj = new StudentServiceCljImpl_clj();
            StudentDO_clj studentDOClj = addStudentViewClj.buildStudentDO();
            boolean addResult = studentServiceClj.add(studentDOClj);
            if (addResult) {
                //重新加载表格，添加最新数据
                mainViewClj.reloadTable();
                addStudentViewClj.dispose();
            } else {
                JOptionPane.showMessageDialog(addStudentViewClj, "添加失败");
            }
        }
    }
}
